﻿namespace Jim.Core;

/// <summary>
/// http请求封装
/// </summary>
public static class HttpExtention
{
    /// <summary>
    /// get请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <returns></returns>
    public static Task<string> GetJson(this string url,Dictionary<string,string>? headers = null)
    {
        return url.DoJsonRequest(item =>
        {
            item.Method = HttpMethod.Get;
        }, headers);
    }

    /// <summary>
    /// post请求-对象
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <param name="postData">json数据</param>
    /// <param name="headers"></param>
    /// <returns></returns>
    public static Task<string> PostJson(this string url, object postData, Dictionary<string, string>? headers = null)
    {
        return url.DoJsonRequest(item =>
        {
            item.Method = HttpMethod.Post;
            item.PostDataType = PostDataType.String;
            item.Postdata = postData.ToJson();
        }, headers);
    }

    /// <summary>
    /// post请求-字符串
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <param name="postData">json数据</param>
    /// <param name="headers"></param>
    /// <returns></returns>
    public static Task<string> PostJson(this string url, string postData, Dictionary<string, string>? headers = null)
    {
        return url.DoJsonRequest(item =>
        {
            item.Method = HttpMethod.Post;
            item.PostDataType = PostDataType.String;
            item.Postdata = postData;
        }, headers);
    }

    /// <summary>
    /// post请求-byte[]
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <param name="postDataBytes">请求数据</param>
    /// <param name="headers"></param>
    /// <returns></returns>
    public static Task<string> PostJson(this string url, byte[] postDataBytes, Dictionary<string, string>? headers = null)
    {
        return url.DoJsonRequest(item =>
        {
            item.Method = HttpMethod.Post;
            item.PostDataType = PostDataType.Byte;
            item.PostdataByte = postDataBytes;
        }, headers);
    }


    /// <summary>
    /// post请求-Form
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <param name="dic">字典</param>
    /// <param name="headers"></param>
    /// <returns></returns>
    public static Task<string> PostJson(this string url, Dictionary<string, string> dic, Dictionary<string, string>? headers = null)
    {
        return url.DoJsonRequest(item =>
        {
            item.Method = HttpMethod.Post;
            item.PostDataType = PostDataType.Form;
            item.PostdataDic = dic;
        }, headers);
    }

    /// <summary>
    /// json请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="item"></param>
    /// <param name="headers"></param>
    /// <returns></returns>
    private static async Task<string> DoJsonRequest(this string url, Action<HttpItem>? item, Dictionary<string, string>? headers=null)
    {
        HttpHelper httpHelper = new HttpHelper();
        HttpItem httpItem = new HttpItem()
        {
            URL = url,
            ContentType = "application/x-www-form-urlencoded; charset=utf-8",
            Accept = "application/x-www-form-urlencoded"
        }; 
        if(item != null) item(httpItem);
        if (headers != null)
        {
            foreach (var header in headers)
            {
                httpItem.Header.Add(header.Key, header.Value);
            }
        }
        HttpResult httpResult = await httpHelper.GetHtml(httpItem);
        return httpResult.Html;
    }
}